<?php

/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class Controller extends CController {
	
	/**
	 *
	 * @var string the default layout for the controller view. Defaults to '//layouts/column1',
	 *      meaning using a single column layout. See 'protected/views/layouts/column1.php'.
	 */
	public $layout = '//layouts/column2';
	
	/**
	 *
	 * @var array context menu items. This property will be assigned to {@link CMenu::items}.
	 */
	public $menu = array ();
	
	/**
	 *
	 * @var array the breadcrumbs of the current page. The value of this property will
	 *      be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
	 *      for more details on how to specify this property.
	 */
	public $breadcrumbs = array ();
	
	/**
	 *
	 * @var mixed 当前登录用户
	 */
	public $admin = null;
	public function init() {
		// 设置语言类型
		if (isset ( $_REQUEST ['lang'] ) && in_array ( $_REQUEST ['lang'], array (
				'zh_cn', 
				'en' 
		) )) {
			Yii::app ()->language = $_REQUEST ['lang'];
		}
	}
	
	/**
	 *
	 * @see CController::__construct($id,$module=null)
	 * @param string $id
	 *        	id of this controller
	 * @param CWebModule $module
	 *        	the module that this controller belongs to.
	 * @param bool $checkAdmin
	 *        	weather to check user's login status, default to false
	 */
	public function __construct($id, $module = null, $checkAdmin = false) {
		parent::__construct ( $id, $module );
		if ($checkAdmin === true && ! self::adminLogin ()) {
			Yii::app ()->request->redirect ( Yii::app ()->createAbsoluteUrl ( 'login', array (
					's' => Yii::app ()->request->url 
			) ), true );
		}
		define ( 'BASE_URL', Yii::app ()->request->getBaseUrl ( true ) );
		define ( 'MEDIA_URL', Yii::app ()->request->getBaseUrl ( true ) );
	}
	
	/**
	 * (non-PHPdoc)
	 *
	 * @see CController::afterRender()
	 */
	public function processOutput($output) {
		if (function_exists ( 'header_remove' )) {
			header_remove ( 'X-Powered-By' );
			header_remove ( 'Server' );
		}
		return parent::processOutput ( $output );
	}
	
	/**
	 * 检查是否已经登录
	 */
	public function adminLogin() {
		$this->admin = User::model ()->find ( 'LOWER(username)=?', array (
				Yii::app ()->user->name 
		) );
		return $this->admin;
		$admin = json_decode ( Yii::app ()->memcache->get ( self::getLoginMKey ( Yii::app ()->user->id ) ), true );
		
		$this->admin = is_array ( $admin ) && $admin ['id'] ? $admin : $this->admin;
		return $this->admin;
	}
	
	/**
	 * 返回登录用户memcache密钥
	 *
	 * @param unknown_type $user_name        	
	 */
	public static function getLoginMKey($username) {
		return md5 ( 'login_user_' . $username );
	}
	
	/**
	 * 返回title
	 *
	 * @see CController::getPageTitle()
	 */
	public function getPageTitle() {
		if ($this->pageTitle !== null) {
			return $this->pageTitle . ' - ' . Yii::app ()->name;
		} else {
			return $this->pageTitle = Yii::app ()->name;
		}
	}
}